/**
 * 题目：
 * 实现 add(1)(2)(3)
 */

export function add(n: number): any {
  let args = Array.prototype.slice.call(arguments);
  const sum = (n: number) => {
    args.push(n);
    return sum;
  };
  sum.toString = () => {
    return args.reduce((pre, next) => {
      return pre + next;
    });
  };

  return sum;
}

/**
 * 封装柯里化函数
 */
export const curry = (fn: Function): Function => {
  let args: any = [];
  let temp = (...params: any) => {
    if (params.length) {
      args = [...args, ...params];
      return temp;
    } else {
      let res = fn.call(null, [...args]);
      args = [];
      return res;
    }
  };
  return temp;
};

function adding(params: any) {
  return [...params].reduce((pre, next) => pre + next);
}

export const myadd = curry(adding);
